// working_module.cpp - 工作的模块示例（单文件版本）
// 这展示了C++20模块的语法，即使某些编译器的模块支持还不完善

#include <iostream>
#include <string>
#include <vector>

// 模拟 export module 语法的效果
namespace WorkingModule {
    // 这些会是 export 的函数
    void greet(const std::string& name) {
        std::cout << "Hello, " << name << " from simulated C++20 module!" << std::endl;
    }
    
    // 这些会是 export 的类
    class ModernCounter {
    private:
        int count;
        
    public:
        ModernCounter() : count(0) {}
        
        void increment() { ++count; }
        void decrement() { --count; }
        int get_count() const { return count; }
        
        void print_count() const {
            std::cout << "Modern Counter value: " << count << std::endl;
        }
    };
    
    // 这些会是 export 的模板
    template<typename T>
    T max_value(T a, T b) {
        return (a > b) ? a : b;
    }
    
    // 这些会是 export 的常量
    constexpr double PI = 3.14159265359;
    constexpr int VERSION = 1;
    
    // 数学运算命名空间
    namespace Math {
        int add(int a, int b) { return a + b; }
        int subtract(int a, int b) { return a - b; }
        double multiply(double a, double b) { return a * b; }
        double divide(double a, double b) {
            if (b == 0.0) throw std::invalid_argument("Division by zero");
            return a / b;
        }
    }
    
    // 字符串工具命名空间
    namespace StringUtils {
        std::string to_upper(std::string str) {
            for (char& c : str) {
                if (c >= 'a' && c <= 'z') {
                    c = c - 'a' + 'A';
                }
            }
            return str;
        }
        
        std::string to_lower(std::string str) {
            for (char& c : str) {
                if (c >= 'A' && c <= 'Z') {
                    c = c - 'A' + 'a';
                }
            }
            return str;
        }
        
        std::vector<std::string> split(const std::string& str, char delimiter) {
            std::vector<std::string> tokens;
            std::string token;
            
            for (char c : str) {
                if (c == delimiter) {
                    if (!token.empty()) {
                        tokens.push_back(token);
                        token.clear();
                    }
                } else {
                    token += c;
                }
            }
            
            if (!token.empty()) {
                tokens.push_back(token);
            }
            
            return tokens;
        }
    }
}

int main() {
    std::cout << "=== 模拟的 C++20 模块示例 ===" << std::endl;
    std::cout << "（展示模块的概念和语法）" << std::endl;
    
    // 在实际的模块中，这会是：import working_module;
    std::cout << "\n--- 基本功能 ---" << std::endl;
    WorkingModule::greet("模块用户");
    
    WorkingModule::ModernCounter counter;
    counter.print_count();
    counter.increment();
    counter.increment();
    counter.increment();
    counter.print_count();
    
    std::cout << "\n--- 模板和常量 ---" << std::endl;
    auto max_int = WorkingModule::max_value(10, 20);
    auto max_double = WorkingModule::max_value(3.14, 2.71);
    std::cout << "max(10, 20) = " << max_int << std::endl;
    std::cout << "max(3.14, 2.71) = " << max_double << std::endl;
    std::cout << "PI = " << WorkingModule::PI << std::endl;
    std::cout << "Version = " << WorkingModule::VERSION << std::endl;
    
    std::cout << "\n--- 数学运算 ---" << std::endl;
    std::cout << "10 + 5 = " << WorkingModule::Math::add(10, 5) << std::endl;
    std::cout << "10 - 5 = " << WorkingModule::Math::subtract(10, 5) << std::endl;
    std::cout << "10 * 5 = " << WorkingModule::Math::multiply(10, 5) << std::endl;
    std::cout << "10 / 5 = " << WorkingModule::Math::divide(10, 5) << std::endl;
    
    std::cout << "\n--- 字符串工具 ---" << std::endl;
    std::string text = "Hello World";
    std::cout << "原始: " << text << std::endl;
    std::cout << "大写: " << WorkingModule::StringUtils::to_upper(text) << std::endl;
    std::cout << "小写: " << WorkingModule::StringUtils::to_lower(text) << std::endl;
    
    auto words = WorkingModule::StringUtils::split("apple,banana,cherry", ',');
    std::cout << "分割 'apple,banana,cherry': ";
    for (size_t i = 0; i < words.size(); ++i) {
        std::cout << words[i];
        if (i < words.size() - 1) std::cout << " | ";
    }
    std::cout << std::endl;
    
    std::cout << "\n=== C++20 模块的真实语法 ===" << std::endl;
    std::cout << "模块接口文件 (.cppm):" << std::endl;
    std::cout << "  export module my_module;" << std::endl;
    std::cout << "  export void my_function();" << std::endl;
    std::cout << "  export class MyClass { ... };" << std::endl;
    std::cout << std::endl;
    std::cout << "使用模块:" << std::endl;
    std::cout << "  import my_module;" << std::endl;
    std::cout << "  my_function();  // 直接使用导出的函数" << std::endl;
    
    return 0;
}
